package com.caiyi.financial.nirvana.sms.gateway;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.queue.ReceiveMsgQueueEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class GatewayReceiveMsgThread implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(GatewayReceiveMsgThread.class);
    private boolean start = true;

    @Override
    public void run() {
        while (start) {
            try {
                List<String> list = JedisService.brpop(5, JedisKey.SMS_QUEUE_RECEIVE_MSG);
                if (list == null || list.isEmpty()) {
                    continue;
                }
                String value = list.get(1);
                logger.info("GatewayReceiveMsgThread run . key : {}, value : {}", list.get(0), value);
                ReceiveMsgQueueEntity receiveMsgQueueEntity = JSON.parseObject(value, ReceiveMsgQueueEntity.class);
                GateWayThread thread = Controller.getGateWayThread(receiveMsgQueueEntity.getGatewayId());
                thread.getGateWay().receiveMsg(receiveMsgQueueEntity);
            } catch (Exception e) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e1) {
                }
                logger.error("GatewayReceiveMsgThread error !!! ", e);
            }
        }

    }
}
